package com.yjd.pub.zhima.service;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.comm.zhima.model.ZhimaCreditDataModel;
import com.yjd.comm.zhima.model.ZhimaCreditModel;
import com.yjd.comm.zhima.service.IZhimaCreditService;
import com.yjd.pub.zhima.dao.IZhimaCreditDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：芝麻信用授权业务实现<br>
 * <b>time</b>：2017-08-11 17:29:25 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("zhimaCreditService")
public class ZhimaCreditServiceImpl extends BaseServiceImpl implements IZhimaCreditService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public ZhimaCreditModel insert(ZhimaCreditModel model) throws Exception {
		return this.zhimaCreditDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(ZhimaCreditModel model) throws Exception {
		return this.zhimaCreditDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.zhimaCreditDao.deleteModel(paramMap, true);
	}

	/**
	 * 芝麻信用授权申请
	 *
	 * @param user_id
	 * @return
	 * @throws Exception
	 */
	@Override
	public ZhimaCreditModel authApply(Long user_id) throws Exception {
		if(user_id==null){
			FrameUtil.throwBaseException1("参数错误");
		}
		UserDataModel userDataModel = userDataService.getModelById(user_id, DbWREnums.WRITE);
		if(userDataModel==null|| StringUtil.isEmpty(userDataModel.getId_name())||StringUtil.isEmpty(userDataModel.getId_number())){
			FrameUtil.throwBaseException1("请先绑定您的银行卡");
		}
		Long currtime = FrameUtil.getTime(null);
		ZhimaCreditModel zhimaCreditModel =	zhimaCreditDao.getModelById(user_id,true);
		if(zhimaCreditModel==null){
			zhimaCreditModel = new ZhimaCreditModel();
			zhimaCreditModel.setUser_id(user_id);
			zhimaCreditModel.setIdcard(userDataModel.getId_number());
			zhimaCreditModel.setIdname(userDataModel.getId_name());
			zhimaCreditModel.setCreate_time(currtime);
			zhimaCreditModel.setUpdate_time(currtime);
			zhimaCreditModel.setStatus(0);
			zhimaCreditDao.insertModel(zhimaCreditModel,true);
		}else {
			zhimaCreditModel.setIdcard(userDataModel.getId_number());
			zhimaCreditModel.setIdname(userDataModel.getId_name());
			zhimaCreditModel.setUpdate_time(currtime);
			zhimaCreditDao.updateModel(zhimaCreditModel,true);
		}
		return zhimaCreditModel;
	}

	@Override
	public ZhimaCreditDataModel zhimaData(Long user_id) throws Exception {

		ZhimaCreditModel zhimaCreditInfo = zhimaCreditDao.getModelOne(FrameUtil.newHashMap("user_id", user_id), DbWREnums.READ.isWrite());

		ZhimaCreditDataModel zhimaInfo = null;
		if(null == zhimaCreditInfo || StringUtil.isEmpty(zhimaCreditInfo.getData())){
			zhimaInfo = new ZhimaCreditDataModel();
			zhimaInfo = new ZhimaCreditDataModel();
			zhimaInfo.setIndustry_level("N/A");
			zhimaInfo.setCredit_score("0");
			zhimaInfo.setFraud_flag("0");
		} else {
			zhimaInfo = JSON.parseObject(zhimaCreditInfo.getData(), ZhimaCreditDataModel.class);
		}

		return zhimaInfo;
	}

	/**
	 * 芝麻授权回调信息
	 *
	 * @param user_id
	 * @param message
	 * @throws Exception
	 */
	@Override
	public void authCallBack(Long user_id, String message) throws Exception {
		if(user_id==null){
			FrameUtil.throwBaseException1("参数错误");
		}

		if(StringUtil.isEmpty(message)){
			FrameUtil.throwBaseException1("授权信息不能为空");
		}

		Long currtime = FrameUtil.getTime(null);
		ZhimaCreditModel zhimaCreditModel =	zhimaCreditDao.getModelById(user_id,true);
		if(zhimaCreditModel==null){
			FrameUtil.throwBaseException1("授权申请记录不存在");
		}

		ZhimaCreditModel newModel = new ZhimaCreditModel();
		newModel.setUser_id(zhimaCreditModel.getUser_id());
		newModel.setUpdate_time(currtime);
		newModel.setStatus(1);
		newModel.setData(message);
		if(zhimaCreditDao.updateModel(newModel,true)!=1){
			FrameUtil.throwBaseException1("授权回调更新失败");
		}
	}

	@Resource
	private IZhimaCreditDao zhimaCreditDao;

	@Resource
	private IUserDataService userDataService;

	@Override
	public IBaseDao getBaseDao() {
		return this.zhimaCreditDao;
	}

}
